Match OP Transform Script

Overview

Matches a node's transform, usually useful for matching a native houdini camera to one in an alembic archive without needing to make direct channel references

Code

import toolutils

## Grab Nodes
scene_viewer = toolutils.sceneViewer()
targetNode = scene_viewer.selectObjects(
                prompt = "Select the target object that will inherit the world transform.",
                allow_multisel = False,
                use_existing_selection=True)[0]
if targetNode is None:
    raise hou.Error("No object was selected.")


## Set parameters
parmName = "target_op"
targetParm = targetNode.parm(parmName)
if targetParm is None:
    ## Create parm if it doesn't exist.
    ptg = targetNode.parmTemplateGroup()
    trackingParm = hou.StringParmTemplate(parmName, "Tracking Node", 1, string_type=hou.stringParmType.NodeReference)
    ptg.append(trackingParm)
    targetNode.setParmTemplateGroup(ptg)
#else: return  ## Don't do anything if it already has a parameter
    
for index, axis in enumerate(("x", "y", "z")):
    for type in ("t", "r", "s"):
        parm = targetNode.parm(type+axis);
        if parm is not None:
            function = {"t": "extractTranslates", "r": "extractRotates", "s": "extractScales"}[type]
            expr = "hou.pwd().parm('{parmName}').evalAsNode().worldTransform().{function}()[{index}]"
            expr = expr.format(parmName=parmName, function=function, index=index)
            
            parm.deleteAllKeyframes()
            parm.setExpression(expr, language=hou.exprLanguage.Python)